<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="(value) => emit('update:modelValue', value)"
    title="巡查报告"
    width="800px"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="120px"
      class="inspection-report-form"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="巡查日期" prop="inspectionDate">
            <el-date-picker
              v-model="form.inspectionDate"
              type="date"
              placeholder="选择巡查日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡查人员" prop="inspector">
            <el-input v-model="form.inspector" placeholder="请输入巡查人员" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="巡查区域" prop="area">
            <el-select v-model="form.area" placeholder="请选择巡查区域" style="width: 100%">
              <el-option label="大坝区域" value="dam" />
              <el-option label="溢洪道" value="spillway" />
              <el-option label="输水隧洞" value="tunnel" />
              <el-option label="库区" value="reservoir" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="天气情况" prop="weather">
            <el-input v-model="form.weather" placeholder="请输入天气情况" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="巡查内容" prop="content">
        <el-input
          v-model="form.content"
          type="textarea"
          :rows="4"
          placeholder="请输入巡查内容"
        />
      </el-form-item>

      <el-form-item label="发现问题" prop="issues">
        <el-input
          v-model="form.issues"
          type="textarea"
          :rows="3"
          placeholder="请输入发现的问题"
        />
      </el-form-item>

      <el-form-item label="处理措施" prop="measures">
        <el-input
          v-model="form.measures"
          type="textarea"
          :rows="3"
          placeholder="请输入处理措施"
        />
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="2"
          placeholder="请输入备注信息"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('update:modelValue', false)">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitting">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'

interface Props {
  modelValue: boolean
  reportData?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  reportData: null
})

const emit = defineEmits<Emits>()

const formRef = ref()
const submitting = ref(false)

const form = reactive({
  id: null,
  inspectionDate: '',
  inspector: '',
  area: '',
  weather: '',
  content: '',
  issues: '',
  measures: '',
  remark: ''
})

const rules = {
  inspectionDate: [
    { required: true, message: '请选择巡查日期', trigger: 'change' }
  ],
  inspector: [
    { required: true, message: '请输入巡查人员', trigger: 'blur' }
  ],
  area: [
    { required: true, message: '请选择巡查区域', trigger: 'change' }
  ],
  content: [
    { required: true, message: '请输入巡查内容', trigger: 'blur' }
  ]
}

// 重置表单
const resetForm = () => {
  Object.assign(form, {
    id: null,
    inspectionDate: '',
    inspector: '',
    area: '',
    weather: '',
    content: '',
    issues: '',
    measures: '',
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 监听报告数据变化
watch(() => props.reportData, (newData) => {
  if (newData) {
    Object.assign(form, {
      id: newData.id || null,
      inspectionDate: newData.inspectionDate || '',
      inspector: newData.inspector || '',
      area: newData.area || '',
      weather: newData.weather || '',
      content: newData.content || '',
      issues: newData.issues || '',
      measures: newData.measures || '',
      remark: newData.remark || ''
    })
  } else {
    resetForm()
  }
}, { immediate: true })

// 提交表单
const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    submitting.value = true
    
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    ElMessage.success('巡查报告保存成功')
    emit('success')
    emit('update:modelValue', false)
  } catch (error) {
    console.error('保存失败:', error)
    ElMessage.error('保存失败，请重试')
  } finally {
    submitting.value = false
  }
}
</script>

<style scoped>
.inspection-report-form {
  padding: 20px 0;
}

.dialog-footer {
  text-align: right;
}
</style>